# -*- coding: utf-8 -*-
# Copyright (C) Nicola Bizzoca   2006/2007  <hannibal_smith@users.sourceforge.net>
# Copyright (C) Simone Economo   2006/2007  <ekoes@users.sourceforge.net>
# 
#    This file is part of Absynth-FTP.
#
#    Absynth-FTP is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    Absynth-FTP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk

class genericTextView:
    
    def __init__(self, features):
        self.__features = features
        self.widget = gtk.TextView()
        self.__buffer = self.widget.get_buffer()
        methods = {
            "editable"    : "set_editable",
            "cursor-blink": "set_cursor_visible",
            "wrap-mode"   : "set_wrap_mode",
            "align-mode"  : "set_justification"
        }
        self.__setWrapMode(self.__features["wrap-mode"])
        self.__setAlignMode(self.__features["align-mode"])
        for property, value in self.__features.items():
            getattr(self.widget, methods[property])(value)
        
    def __setWrapMode(self, mode):
        wrap = {
            "none": gtk.WRAP_NONE,
            "char": gtk.WRAP_CHAR,
            "word": gtk.WRAP_WORD,     
        }
        self.__features["wrap-mode"] = wrap[mode]
        
    def __setAlignMode(self, mode):
        align = {
            "right" : gtk.JUSTIFY_RIGHT,
            "left"  : gtk.JUSTIFY_LEFT,
            "center": gtk.JUSTIFY_CENTER,
        }
        self.__features["align-mode"] = align[mode]
        
    def writeText(self, message, addbreak = True, sep = None):
        linesep = True and sep or ""
        linebreak = addbreak and "\n" or ""
        self.__buffer.insert(self.__buffer.get_end_iter(), "%s%s%s" % (message, linesep, linebreak))
        self.widget.scroll_to_mark(self.__buffer.get_insert(), 0)
        
    def writeTextLines(self, lines, addbreak = True, sep = None):
        for line in lines:
            self.writeText(line, addbreak, sep)
            
    def setLineHeight(self, above, below):
        self.widget.set_pixels_above_lines(above)
        self.widget.set_pixels_below_lines(below)
        
    def setMargin(self, left, right):
        self.widget.set_left_margin(left)
        self.widget.set_right_margin(right)
        
    def setScrollbar(self, hmode = "auto", vmode = "auto"):
        policy = {
            "auto": gtk.POLICY_AUTOMATIC,
            "show": gtk.POLICY_ALWAYS,
        }
        self.scrollbar = gtk.ScrolledWindow()
        self.scrollbar.set_policy(policy[hmode], policy[vmode])
        self.scrollbar.add(self.widget)
        
class testSuite:
    
    def __init__(self):
        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.connectEvents(self.window, {
             "delete_event" : self.delete,
             "destroy": self.destroy,
        })
        
        self.textview = genericTextView({
            "editable"    : False,
            "cursor-blink": False,
            "wrap-mode"   : "word",
            "align-mode"  : "left",
        })
        self.textview.setMargin(30, 60)
        self.textview.setScrollbar()
        self.textview.scrollbar.set_size_request(300, 200)

        self.textview.writeTextLines(
            ["%s è l'equivalente di %s" % (numb+32, chr(numb+32)) for numb in range(1,10)],
            True,
            " , mentre")
        
        vbox, button = gtk.VBox(), gtk.Button("asderello")
        button.connect("clicked", self.callback, "asdrubale")
        vbox.add(self.textview.scrollbar)
        vbox.add(button)
        
        self.window.add(vbox)
        self.window.show_all()
          
    def callback(self, widget, data):
        self.textview.writeText("Hello again - %s was pressed" % data)
        
    def connectEvents(self, widget, events):
        for type,callback in events.items():
            widget.connect(type, callback)

    def delete(self, widget, event, data=None):
        #print "delete event occurred"
        return False

    def destroy(self, widget, data=None):
        #print "destroy signal occurred"
        gtk.main_quit()
        
    def runTest(self):
        gtk.main()
        
if __name__ == "__main__": 
    GTK = testSuite()
    GTK.runTest()  